import React, { useCallback } from 'react';
import { Alert, Empty, Skeleton } from 'antd';
import { defaultFormatResult, useRequest } from '@/hooks';
import {
    getPersonalDoc,
    saveEmployeeBasicInfo,
    saveEmployeeEducationalData,
    saveEmployeeWorkData,
    saveLanguages,
    saveSkill,
    saveEmployeeAwardData,
    saveEmployeeTrainingData
} from '@/pages/Organization/EmployeeManage/services';
import PersonalInfoSection from '@/pages/Organization/EmployeeManage/components/EmployeeInfoSections/PersonalInfoSection';
import WorkExperienceSection from '@/pages/Organization/EmployeeManage/components/EmployeeInfoSections/WorkExperienceSection';
import EducationalExperienceSection from '@/pages/Organization/EmployeeManage/components/EmployeeInfoSections/EducationalExperienceSection';
import LanguagesSection from '@/pages/Organization/EmployeeManage/components/EmployeeInfoSections/LanguagesSection';
import SkillSection from '@/pages/Organization/EmployeeManage/components/EmployeeInfoSections/SkillSection';
import TrainingExperienceSection from '@/pages/Organization/EmployeeManage/components/EmployeeInfoSections/TrainingExperienceSection';
import AwardRecordSection from '@/pages/Organization/EmployeeManage/components/EmployeeInfoSections/AwardRecordSection';
import authHelper from '@/utils/auth/helper';

/**
 * 员工详情 - 个人信息
 */
const PersonalTabContent = ({ employeeCode, isPersonalPage, employeeInfoData }) => {
    // 获取数据
    const {
        data: employeeArchivesData,
        mutate: employeeArchivesInfoMutate,
        error: fetchEmployeeArchivesInfoError,
        loading: fetchEmployeeArchivesInfoLoading
    } = useRequest(() => getPersonalDoc({ employeeCode }), {
        cacheKey: `employee-personal-${employeeCode}`,
        refreshDeps: [employeeCode],
        ...defaultFormatResult
    });
    // 更新数据, 这个页面有多个部分, 每个部分数据单独在组件内部维护, 保存之后会触发父级组件刷新数据.
    const handleUpdateEmployeeData = useCallback(
        changedData => {
            employeeArchivesInfoMutate(oldDta => ({ ...oldDta, ...changedData }));
        },
        [employeeArchivesInfoMutate]
    );

    // 错误处理
    if (fetchEmployeeArchivesInfoError || (!fetchEmployeeArchivesInfoLoading && !employeeArchivesData)) {
        return <Alert type="error" message={fetchEmployeeArchivesInfoError?.msg || '员工数据加载失败'} />;
    }

    return (
        <Skeleton loading={!employeeArchivesData || fetchEmployeeArchivesInfoLoading} active>
            {employeeArchivesData ? (
                <>
                    {/* 个人信息 */}
                    <PersonalInfoSection
                        readonly={(!isPersonalPage && !authHelper.checkCode('office:orgs:employee:detail:updateBase')) || employeeInfoData?.serviceStatus === 6}
                        formData={employeeArchivesData}
                        onChange={handleUpdateEmployeeData}
                        saveAction={saveEmployeeBasicInfo}
                    />
                    {/* 教育经历 */}
                    <EducationalExperienceSection
                        readonly={!authHelper.checkCode('office:orgs:employee:detail:updateuserschool') || employeeInfoData?.serviceStatus === 6}
                        formData={employeeArchivesData}
                        onChange={handleUpdateEmployeeData}
                        saveAction={saveEmployeeEducationalData}
                    />
                    {/* 工作经历 */}
                    <WorkExperienceSection
                        readonly={!authHelper.checkCode('office:orgs:employee:detail:updateuserwork') || employeeInfoData?.serviceStatus === 6}
                        formData={employeeArchivesData}
                        onChange={handleUpdateEmployeeData}
                        saveAction={saveEmployeeWorkData}
                    />
                    {/* 培训经历 */}
                    <TrainingExperienceSection
                        readonly={
                            (!isPersonalPage && !authHelper.checkCode('office:orgs:employee:detail:updateusertrain'))
                            || employeeInfoData?.serviceStatus === 6
                        }
                        formData={employeeArchivesData}
                        onChange={trainList => handleUpdateEmployeeData({ trainList })}
                        saveAction={saveEmployeeTrainingData}
                    />
                    {/* 获奖记录 */}
                    <AwardRecordSection
                        readonly={
                            (!isPersonalPage && !authHelper.checkCode('office:orgs:employee:detail:updateuseraward'))
                            || employeeInfoData?.serviceStatus === 6
                        }
                        formData={employeeArchivesData}
                        onChange={awardList => handleUpdateEmployeeData({ awardList })}
                        saveAction={saveEmployeeAwardData}
                    />
                    {/* 语言能力列表 */}
                    <LanguagesSection
                        readonly={
                            (!isPersonalPage && !authHelper.checkCode('office:orgs:employee:detail:updateLanguage'))
                            || employeeInfoData?.serviceStatus === 6
                        }
                        formData={employeeArchivesData}
                        onChange={langList => handleUpdateEmployeeData({ langList })}
                        saveAction={saveLanguages}
                    />
                    {/* 专业技能 */}
                    <SkillSection
                        readonly={(!isPersonalPage && !authHelper.checkCode('office:orgs:employee:detail:updateSkill')) || employeeInfoData?.serviceStatus === 6}
                        formData={employeeArchivesData}
                        onChange={skillList => handleUpdateEmployeeData({ skillList })}
                        saveAction={saveSkill}
                    />
                </>
            ) : (
                <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} />
            )}
        </Skeleton>
    );
};

export default PersonalTabContent;
